package com.jcl.core.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FilterUtils

 * @Author yyliu
 * @Date 2018/9/5 13:51
 * @Version 1.0
 **/
public class FilterUtils {

    private static final Logger logger = LoggerFactory.getLogger(FilterUtils.class);

    private static String crawlerUrl = "/api/crawler";
    private static String testUrl = "/api/test";
    private static String unsetUrl = "/unset/";
    private static List<String> whiteSecurityIp = new LinkedList<>();

    static {
        whiteSecurityIp.add("/api/wechat");
        whiteSecurityIp.add("/api/wechat/access");
        whiteSecurityIp.add("/api/wechat/to/access");
        whiteSecurityIp.add("/api/wechat/try/access");
        whiteSecurityIp.add("/api/wechat/register/corp");
        whiteSecurityIp.add("/api/wechat/register/inviteuser");
        whiteSecurityIp.add("/api/wechat/login");
        whiteSecurityIp.add("/api/wechat/login_token");
        whiteSecurityIp.add("/api/wechat/login/corplist/access");
        whiteSecurityIp.add("/api/wechat/login/corplist");
        whiteSecurityIp.add("/api/wechat/share/resume/detail");
        whiteSecurityIp.add("/api/wechat/signature");

        whiteSecurityIp.add("/api/verify/email/registercode");
        whiteSecurityIp.add("/api/verify/email/registercode/validate");

        whiteSecurityIp.add("/api/searchresume/save");
        whiteSecurityIp.add("/api/searchresume/projectsave");
        whiteSecurityIp.add("/api/searchresume/worksave");
        whiteSecurityIp.add("/api/searchresume/edusave");
        whiteSecurityIp.add("/api/searchresume/keywords");

        whiteSecurityIp.add("/api/resume/save");
        whiteSecurityIp.add("/api/resume/projectsave");
        whiteSecurityIp.add("/api/resume/worksave");
        whiteSecurityIp.add("/api/resume/edusave");
        whiteSecurityIp.add("/api/resumeorig/save");
        whiteSecurityIp.add("/api/resume/tooa");

        whiteSecurityIp.add("/api/test/el");
        whiteSecurityIp.add("/api/oaaccess");
        whiteSecurityIp.add("/api/spring/redis");
        whiteSecurityIp.add("/api/user/login");
        whiteSecurityIp.add("/api/user/login_token");

    }

    /* *
     * @Author yyliu
     * @Date 13:54 2018/9/5
     * @Param [filter, request]
     * @return void
     **/
    public static void printRequest(Filter filter, HttpServletRequest request) {
        logger.info("Request URL:" + request.getRequestURI());
        logger.info("Request IP:" + RequestUtils.getClientIpAddress(request));
        Map<String, ?> paramMap = request.getParameterMap();
        if (null != paramMap && paramMap.size() > 0) {
            logger.info("Request param is:");
            for (Map.Entry<String, ?> entry : paramMap.entrySet()) {
                if (null != entry.getValue() && entry.getValue() instanceof String[]) {
                    logger.info(entry.getKey() + "={");
                    for (String str : (String[]) entry.getValue()) {
                        logger.info(str);
                    }
                    logger.info("}");
                } else {
                    logger.info(entry.getKey() + "=" + entry.getValue());
                }
            }

        }
    }

    /* *
     * @Author yyliu
     * @Description 安全请求过滤
     * @Date 14:24 2018/9/19
     * @Param [req]
     * @return boolean
     **/
    public static boolean isSecurityUrl(HttpServletRequest req) {
        String url = req.getRequestURI();
        if (whiteSecurityIp.contains(url)) {
            return true;
        }
        if (url.indexOf(testUrl) > -1) {
            return true;
        }
        return false;
    }

    /* *
     * @Author yyliu
     * @Description 安全请求过滤
     * @Date 14:24 2018/9/19
     * @Param [req]
     * @return boolean
     **/
    public static boolean isCrawlerUrl(HttpServletRequest req) {
        String url = req.getRequestURI();
        if (url.indexOf(crawlerUrl) > -1) {
            return true;
        }
        return false;
    }

    /* *
     * @Author yyliu
     * @Description 安全请求过滤
     * @Date 14:24 2018/9/19
     * @Param [req]
     * @return boolean
     **/
    public static boolean unsetUrl(HttpServletRequest req) {
        String url = req.getRequestURI();
        if (url.indexOf(unsetUrl) > -1) {
            return true;
        }
        return false;
    }

}
